package com.ui;

import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class pur_history extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTable table1;

    public pur_history() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // 点击 X 时调用 onCancel()
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // 遇到 ESCAPE 时调用 onCancel()
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }


    private void onOK() {
        DENGLU.c.process(DENGLU.c.Check());
        // 在此处添加您的代码

    }

    private void onCancel() {
        // 必要时在此处添加您的代码
        dispose();
    }

    public void main() {
        DENGLU.c.history.clear();
        String[] COLUMNAMES=new String[]{"记录单","商品号","价格","数量","商品名称"};
        //String[][] COLUMNAMES2=new String[][]{{"1","2","3","4","5","6"},{"6","5","4","3","2","1"}};
        //String[][] COLUMNAMES2=new String[][]{};

        for(int i=1;i<=15;i++)
        {
            DENGLU.c.process(DENGLU.c.GetHistory(i));
        }
        for(String key:DENGLU.c.history.keySet()){

        }
        System.out.println(DENGLU.c.history);
        String[][] a=new String[DENGLU.c.history.keySet().size()][5];
        int i=0;
        for(String key:DENGLU.c.history.keySet())
        {
            a[i][0]=key;
            a[i][1]=DENGLU.c.history.get(key).getID();
            a[i][2]=String.valueOf(DENGLU.c.history.get(key).getPrice());
            a[i][3]=String.valueOf(DENGLU.c.history.get(key).getNum());
            a[i][4]=String.valueOf(DENGLU.c.history.get(key).getName());
            i++;
        }
        JTable table1=new JTable(a,COLUMNAMES);
        JScrollPane scrollPane=new JScrollPane(table1);

        pur_history dialog = new pur_history();
        dialog.pack();
        dialog.setContentPane(scrollPane);
        dialog.setVisible(true);


        //System.exit(0);
    }
}
